//
//  SaiViewController.m
//  多玩盒子
//
//  Created by 刘胜 on 16/2/26.
//  Copyright © 2016年 刘胜. All rights reserved.
//

#import "SaiViewController.h"
#import "TopModel.h"
#import "RootTableViewCell.h"
#import "LSZNetWorkTools.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface SaiViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView * tablView;
@property(nonatomic,strong)NSMutableArray * allArray;

@end

@implementation SaiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.allArray = [NSMutableArray array];
    [self creatTableView];
    [self getData];
    
    
}

-(void)getData {
    NSString * string = @"http://box.dwstatic.com/apiNewsList.php?action=l&newsTag=upgradenews&p=1&v=166&OSType=iOS9.1&versionName=2.5.1%20";
 NSString *encoded = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [LSZNetWorkTools getWithUrl:encoded parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        NSMutableArray * array = [result objectForKey:@"data"];
                for (NSMutableDictionary * dic in array) {
                     TopModel * model = [TopModel modelWithDictionary:dic];
                    [self.allArray addObject:model];
                    NSLog(@"%@",model.title);
        
                }
                 [_tablView reloadData];
        
    } fail:^(NSError *error) {
        
        
    }];
    
    
    
    
}

-(void)creatTableView {
    _tablView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40,WIDTH , HEIGHT) style:UITableViewStyleGrouped];
    _tablView.delegate = self;
    _tablView.dataSource = self;
    [_tablView registerClass:[RootTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tablView];
    [_tablView reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RootTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.cellModel = [self.allArray objectAtIndex:indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return HEIGHT/7;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
